package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;
	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;
	@Autowired
	private RechargeInfoMapper rechargeInfoMapper;

	/**
	 * 初始化用户账户
	 * @param userId
	 */
	@Override
	public void addUserAccount(Long userId) {
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(userId);
		userAccount.setTotalAmount(new BigDecimal(1000));
		userAccount.setAvailableAmount(new BigDecimal(1000));
		userAccountMapper.insert(userAccount);
	}

	/**
	 * 获取用户账户信息
	 * @param userId
	 * @return
	 */
    @Override
    public BigDecimal getAvailableAmount(Long userId) {
		UserAccount userAccount = userAccountMapper.selectOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId).select(UserAccount::getAvailableAmount));
		return userAccount.getAvailableAmount();
    }

	/**
	 * 扣减用户账户金额
	 * @param accountDeductVo
	 * @return
	 */
    @Override
    public boolean checkAndDeduct(AccountDeductVo accountDeductVo) {
		int result = userAccountMapper.checkAndDeduct(accountDeductVo.getUserId(), accountDeductVo.getAmount());

		if (result>0) { //数据库有修改 行数 即扣减了 用户 账户金额
			//记录当前账户资金流向
			this.addUserAccountDetail(accountDeductVo.getUserId(),
					accountDeductVo.getContent(),
					SystemConstant.ACCOUNT_TRADE_TYPE_MINUS,
					accountDeductVo.getAmount(),
					accountDeductVo.getOrderNo()
					);
			//返回true
			return true;
		}

		return  false;
    }

	/**
	 * //扣减用户账户金额后，记录当前账户资金流向 至user_account_detail 用户账户明细表
	 * @param userId
	 * @param content
	 * @param tradeType
	 * @param amount
	 * @param orderNo
	 */
	private void addUserAccountDetail(Long userId, String content, String tradeType, BigDecimal amount, String orderNo) {
		//用户账户明细表
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		//赋值传入的数据到 用户账户明细 对象
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(content);
		userAccountDetail.setTradeType(tradeType);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);
		//记录当前账户资金流向
		userAccountDetailMapper.insert(userAccountDetail);
	}

	/**
	 * 充值成功通知
	 * @param orderNo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void rechargePaySuccess(String orderNo) {
		//充值成功后：修改状态，增加余额 记录充值记录
		// 获取到充值信息对象
		RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo, orderNo));
		// 如果当前状态是已支付，则直接返回
		if (SystemConstant.ORDER_STATUS_PAID.equals(rechargeInfo.getRechargeStatus())) return;
		// 否则赋值为已支付状态
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
		// 更新已支付状态的数据
		this.rechargeInfoMapper.updateById(rechargeInfo);

		// 更新余额
		userAccountMapper.updateUserAccount(rechargeInfo.getUserId(),rechargeInfo.getRechargeAmount());

		//记录充值记录
		this.addUserAccountDetail(rechargeInfo.getUserId(),"充值",SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT,rechargeInfo.getRechargeAmount(),rechargeInfo.getOrderNo());
	}

	/**
	 * 用户充值记录分页列表
	 * @param pageParam
	 * @param userId
	 * @return
	 */
	@Override
	public IPage<UserAccountDetail> findUserRechargePage(Page<UserAccountDetail> pageParam, Long userId) {
		//1201
		return userAccountDetailMapper.selectPage(pageParam,new LambdaQueryWrapper<UserAccountDetail>().eq(UserAccountDetail::getUserId,userId).eq(UserAccountDetail::getTradeType,SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT));
	}

	/**
	 * 用户消费记录分页列表
	 * @param pageParam
	 * @param userId
	 * @return
	 */
	@Override
	public IPage<UserAccountDetail> findUserConsumePage(Page<UserAccountDetail> pageParam, Long userId) {
		// 1204
		return userAccountDetailMapper.selectPage(pageParam,new LambdaQueryWrapper<UserAccountDetail>().eq(UserAccountDetail::getUserId,userId).eq(UserAccountDetail::getTradeType,SystemConstant.ACCOUNT_TRADE_TYPE_MINUS));
	}
}





















